package com.it.data_linkedList.leetcode;

/**
 * @Author: Coke
 * @DateTime: 2023/06/04/21:51
 * @注释: 链表节点类
 **/
public class ListNode {
    public Integer val;
    public ListNode next;
    
    public ListNode (int val, ListNode next) {
        this.val = val;
        this.next = next;
    }
    
    /**
     * @param arr:
     * @return ListNode
     * @作者: Coke
     * @注释: 创建链表
     * @DateTime: 2023/6/6 22:40
     */
    public static ListNode of (int... arr) {
        ListNode node = new ListNode(arr[arr.length - 1], null);
        for (int i = arr.length - 2; i >= 0; i--) {
            ListNode p = new ListNode(arr[i], null);
            p.next = node;
            node = p;
        }
        return node;
    }
    
    
    @Override
    public String toString () {
        StringBuilder builder = new StringBuilder();
        builder.append("[");
        ListNode node = this;
        while (node != null) {
            builder.append(node.val);
            if (node.next != null) {
                builder.append(",");
            }
            node = node.next;
        }
        builder.append("]");
        return builder.toString();
    }
}
